package com.woniuxy.ams.controller;

import com.woniuxy.ams.entity.ResultEntity;
import com.woniuxy.ams.entity.Role;
import com.woniuxy.ams.entity.User;
import com.woniuxy.ams.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author fyx
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @RequestMapping("/findAllRoles")
    public ResultEntity<?> findAll(@RequestBody(required = false) Role role) {
        List<Role> roles = roleService.findAllRolesAndSearch(role);
        if (roles.size() != 0) {
            return new ResultEntity<>(200, "查询成功", roles);
        }else {
            return new ResultEntity<>(400, "查询失败",null);
        }
    }
@RequestMapping("/findAllRolesByUid")
    public ResultEntity<?> findRolesByUserId(@RequestBody int id) {
        List<Integer> roles = roleService.getRoles(id);
        return new ResultEntity<>(200, "查询成功", roles);
    }

    @RequestMapping("/setRoles")
    public ResultEntity<?> setRoles(@RequestBody Params params) {
        boolean isSuccess =  roleService.setRoleByUser(params.user,params.roles);
        if (isSuccess) {
            return new ResultEntity<>(200, "添加成功");
        }
        return new ResultEntity<>(200, "添加失败");
    }
    private static class Params {
        public User user;
        public List<Role> roles;
    }
}
